package com.ala4.oxcafe.controller;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.boot.page.PageResult;
import com.ala4.oxcafe.domain.R;
import com.ala4.oxcafe.dto.ResetPasswordDTO;
import com.ala4.oxcafe.dto.UserRoleRelDTO;
import com.ala4.oxcafe.entity.MenuInfo;
import com.ala4.oxcafe.entity.UserInfo;
import com.ala4.oxcafe.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息controller
 *
 * @author PING
 * @version 1.0.0
 * @date 2025/8/12 14:20
 */
@RestController
@RequestMapping("/user-info")
@AllArgsConstructor
@Tag(name = "用户管理", description = "用于管理用户信息")
public class UserInfoController {

    private final UserInfoService userInfoService;

    @Operation(summary = "分页查询用户信息")
    @GetMapping("/page")
    public R<PageResult<UserInfo>> page(PageDomain pageDomain, UserInfo userInfo) {
        return R.ok(PageResult.iPage(userInfoService.userInfoPage(pageDomain, userInfo)));
    }

    @Operation(summary = "创建用户")
    @PostMapping("/create")
    public R<UserInfo> create(@RequestBody @Validated(UserInfo.Create.class) UserInfo userInfo) {
        return R.ok(userInfoService.createUserInfo(userInfo));
    }

    @Operation(summary = "更新用户")
    @PutMapping("/update")
    public R<UserInfo> update(@RequestBody @Validated(UserInfo.Update.class) UserInfo userInfo) {
        return R.ok(userInfoService.updateUserInfo(userInfo));
    }

    @Operation(summary = "用户重置密码(需要验证旧密码)")
    @PutMapping("/user-reset-password")
    public R<?> userResetPasswor(@RequestBody @Validated(ResetPasswordDTO.UserReset.class) ResetPasswordDTO resetPasswordDTO) {
        userInfoService.resetPassword(resetPasswordDTO, true);
        return R.ok();
    }

    @Operation(summary = "管理员重置密码(不需要验证旧密码)")
    @PutMapping("/admin-reset-password")
    public R<?> adminResetPasswor(@RequestBody @Validated(ResetPasswordDTO.AdminReset.class) ResetPasswordDTO resetPasswordDTO) {
        userInfoService.resetPassword(resetPasswordDTO, false);
        return R.ok();
    }

    @Operation(summary = "用户分配角色")
    @PutMapping("/allocation")
    public R<?> allocation(@RequestBody @Validated UserRoleRelDTO userRoleRelDTO) {
        userInfoService.allocation(userRoleRelDTO);
        return R.ok();
    }

    @Operation(summary = "查询当前用户菜单")
    @GetMapping("/user-menu")
    public R<List<MenuInfo>> userMenu() {
        return R.ok(userInfoService.userMenu());
    }

    @Operation(summary = "查询当前用户菜单-v2")
    @GetMapping("/user-menu-v2")
    public R<List<String>> userMenuV2() {
        return R.ok(userInfoService.userMenuV2());
    }

    @Operation(summary = "用户详情")
    @GetMapping("/{userId}")
    public R<UserInfo> info(@PathVariable("userId") Long userId) {
        return R.ok(userInfoService.userInfo(userId));
    }
}
